package com.zqwh.xtool.utils;

import android.text.TextUtils;

import com.zqwh.yj.sdk.utils.CommonUtils;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.xtool.domain.StockVO;
import com.zqwh.xtool.domain.ToolVO;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static com.zqwh.xtool.ToolsApp.gToolModel;

public class ZqwhUtils {

    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static boolean isRfidStockHasLent(StockVO stock, List<String> lsTagList){
        for (String epc:lsTagList){
            if (stock.getEpc() != null && stock.getEpc().equals(epc)){
                return false;
            }
        }
        return true;
    }

    public static boolean isNoRfidStockHasLent(StockVO stock, List<StockVO> pNoRfidStockList){
        for (StockVO s:pNoRfidStockList){
            if (stock.getStockId() != null && stock.getStockId().equals(s.getStockId())){
                return false;
            }
        }
        return true;
    }

    public static String stockList2StockIds(List<StockVO> stockList){
        String result = null;
        if(stockList != null){
            for (int i = 0; i < stockList.size(); i++){
                if(i == 0){
                    result = String.valueOf(stockList.get(i).getStockId());
                }else {
                    result = result+","+stockList.get(i).getStockId();
                }
            }
        }
        return result;
    }

    /**
     * 将标签信息转换为工具信息
     * @param lsTagList
     */
    public static void epcs2tools(List<String> lsTagList, final ZqwhTransListener listener){
        String epcs = CommonUtils.listTagToString(lsTagList);
        gToolModel.epcs2tools(epcs, new NetCallback<List<ToolVO>>() {
            @Override
            public void onSuccess(List<ToolVO> tools) {
                listener.epcs2toolsFinished(tools);
            }

            @Override
            public void onError(String msg) {
            }
        });
    }

    public static boolean isEpcInStockList(String epc, List<StockVO> stockVOList){
        for (StockVO stock : stockVOList){
            if (stock.getEpc().equals(epc)){
                return true;
            }
        }
        return false;
    }

    public static boolean isStockInEpcList(StockVO stock, List<String> epcList){
        for (String epcTmp : epcList){
            if (epcTmp.equals(stock.getEpc())){
                return true;
            }
        }
        return false;
    }

    public static List<String> removeCount(List<String> tags){
        List<String> tagsRemoved = new ArrayList<>();
        for (String tag:tags){
            tagsRemoved.add(tag.split("#")[0]);
        }
        return tagsRemoved;
    }

    public static String genKey(Long devId) {
        Random random = new Random();
        int x = random.nextInt(899999);
        return devId + "-" + System.currentTimeMillis() + "-" + nextInt(100000, 999999);
    }

    public static int nextInt(final int min, final int max){
        Random rand= new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (max - min + 1) + min;
    }
}
